#include <vector>
#include <iostream>
using std::vector; using std::cout; using std::endl;

#include "sprite.h"
#include "sound.h"

Sprite::Sprite(const Vector2f& pos, const Vector2f& vel, const Frame* f) :
  Drawable(pos, vel), 
  gameData(Gamedata::getInstance()),
  frame(f)
  { }

Sprite::Sprite(const Sprite& s) :
  Drawable(s.position, s.velocity), 
  gameData(Gamedata::getInstance()),
  frame(s.frame)
  { }
Sprite::~Sprite(){
	//std::cout<<"delete sprite"<<std::endl;

}
Sprite& Sprite::operator=(const Sprite& rhs) {
  position = rhs.position;
  velocity = rhs.velocity;
  initialVelocity = rhs.initialVelocity;
  frame = rhs.frame;
  return *this;
}

void Sprite::draw() const { 
  Uint32 x = static_cast<Uint32>(X());
  Uint32 y = static_cast<Uint32>(Y());
  frame->draw(x, y); 
}

void Sprite::update(unsigned ticks) { 
  float incr = velocityY() * static_cast<float>(ticks) * 0.001;
  Y( Y()+incr );
  float height = static_cast<float>(frame->getHeight());
  if ( Y() < 0) {
    velocityY( abs( velocityY() ) );
    //SDLSound::getInstance()->operator [](0);
  }	
  if ( Y() > gameData->getXmlInt("worldHeight")-height) {
    velocityY( -abs( velocityY() ) );
    //SDLSound::getInstance()->operator [](0);
  }

  incr = velocityX() * static_cast<float>(ticks) * 0.001;
  X( X()+incr );
  float width = static_cast<float>(frame->getWidth());
  if ( X() < 0) {
    velocityX( abs( velocityX() ) );
    //SDLSound::getInstance()->operator [](0);
  }
  if ( X() >  gameData->getXmlInt("worldWidth")-width) {
    velocityX( -abs( velocityX() ) );
    //SDLSound::getInstance()->operator [](0);
  }
}

unsigned Sprite::getPixel(Uint32 i, Uint32 j) const { 
  Uint32 x = static_cast<Uint32>(X());
  Uint32 y = static_cast<Uint32>(Y());
  x = i - x;
  y = j - y;
  Uint32 *pixels = static_cast<Uint32 *>(frame->getSurface()->pixels);
  return pixels[ ( y * frame->getWidth() ) + x ];
}

